mutate(penguins, mean = mean(bill_length_mm, na.rm = TRUE))Error in mutate(penguins, mean = mean(bill_length_mm, na.rm = TRUE)): could not find function "mutate"
Day 1 - Introduction to Data Analysis with R
Freie Universität Berlin - Theoretical Ecology
October 2, 2023
When learning a programming language, you have to be prepared to spend a lot of your time with fixing bugs in the code. So don’t worry: It’s not you, it’s just how programming works! .footnote-right[Artwork by Allison Horst]
Debugging can be annoying and we can’t avoid it, but …
… it’s an effective learning experience (I actually learned the most from debugging my code)
… it will get easier over time
… there are some debugging techniques to decrease the time in stages 2-7
… if nothing helps, there are great people all over the internet willing to help
and how to deal with them class: inverse, center, middle
. . .
. . .
Could not find function errors have two main reasons:
library() or call the function with packageName::functionName()lenght() instead of length())x not foundError in eval(expr, envir, enclos): object 'hello' not found
Error in eval(expr, envir, enclos): object 'variable_A' not found
. . .
variableA but you try to access variable_A)print(hello) looks for a variable named hello but instead you wanted to print the string print("hello")does not necessarily trigger an error message
if there is an error message, it can also appear later in your code
. . .
str() of your data and check whether all columns are there and in correct format
character but should be of type integer?Sometimes R crashes completely and you see this:
There is no fix but to start a new session
Make sure to save your scripts regularly!
+R is not running code anymore and the console only prints + if you try to execute a command.
Escape. Then you should see the > sign instead of + again.R can give you warnings for many reasons, e.g.
NA values in your data and try to plot themNAWarnings are no errors and can sometimes be ignored but:
A step by step guide
Often, you don’t need to do all the steps but a systemmatic approach to bug fixing is very helpful.
Step 1: Carefully read the error message and try to fix it
Step 2: Is it any of the errors you learned about just now?
Step 3: If the error is about data or other variables: look at the structure using str()
Step 4: If the error is about a function: Read the documentation using ?functionName. - Did you use the function correctly? - Did you forget an argument?
Step 5: Look for answers online - often you can also jump directly to this step
Step 6: Ask others for help
Tip
Change language of R messages to English with Sys.setenv(LANGUAGE='en')
There are plenty of places where you can ask for help online. Some common and good options are:
Ask a question on Stack Overflow
Ask in the R Discord server
But: You have to make sure that before, you tried all the other 5 steps.
To ask questions online, you have to learn how to ask a good R question. This includes:
Look here for more info on how to ask a good question about R
Selina Baldauf // Common errors